package logic

import (
	"context"

	domain "go-weixin/app/diary/rpc/internal/doamin"
	"go-weixin/app/diary/rpc/internal/svc"
	"go-weixin/app/diary/rpc/pb/proto/diary"

	"github.com/zeromicro/go-zero/core/logx"
)

type DiaryDetailLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
	diaryDomain *domain.DiaryDomain
}

func NewDiaryDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DiaryDetailLogic {
	return &DiaryDetailLogic{
		ctx:         ctx,
		svcCtx:      svcCtx,
		Logger:      logx.WithContext(ctx),
		diaryDomain: domain.NewDiaryDomain(*svcCtx.ChenDb),
	}
}

func (l *DiaryDetailLogic) DiaryDetail(in *diary.DiaryDetailReq) (*diary.DiaryDetailResp, error) {
	d, err := l.diaryDomain.DiaryDetail(in.Id)
	if err != nil {
		return nil, err
	}

	return &diary.DiaryDetailResp{
		Diary: &diary.DiaryBase{
			Id:       d.Id,
			UserId:   d.UserId,
			Title:    d.Title,
			Content:  d.Content,
			CreateAt: d.CreateAt.Unix(),
			UpdateAt: d.UpdateAt.Unix(),
			DeleteAt: d.DeleteAt.Unix(),
		},
	}, nil
}
